<template>
  <div class="pages">
    <div class="step-line" v-if="!checkFlag">
      <el-steps :active="activeNumber" align-center finish-status="success">
        <el-step title="创建活动"></el-step>
        <el-step title="设置优惠券"></el-step>
        <el-step title="选择商品"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
    </div>
    <el-form :rules="rules" :model="form" size="small" label-width="120px">
      <div class="base-info" v-if="(activeNumber=== 1 || activeNumber=== 2) && !checkFlag">
        <div>活动名称: {{form.activityName}}</div>
        <div>活动时间: {{form.activityStartTime}}至{{form.activityEndTime}}</div>
        <div>创建时间: {{createdTime}}</div>
      </div>
      <div v-if="activeNumber=== 0 && !checkFlag">
        <el-form-item label="活动名称" prop="activityName">
          <el-input
            v-model="form.activityName"
            size="small"
            maxlength="10"
            show-word-limit
            @keyup.native="limitName"
          ></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="activityStartTime">
          <el-date-picker
            v-model="form.activityStartTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
            @change="checkTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="activityEndTime">
          <el-date-picker
            v-model="form.activityEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            @change="checkTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="定向人群" prop="activityCrowd">
          <el-radio-group v-model="form.activityCrowd">
            <el-radio label="0">全部会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动说明">
          <el-input type="textarea" v-model="form.activityDesc"></el-input>
        </el-form-item>
      </div>
      <div v-if="activeNumber=== 1 && !checkFlag" class="second-step">
        <el-form-item label="优惠券类型" prop="ticketType">
          <el-radio label="4" v-model="form.ticketType">折扣券</el-radio>
        </el-form-item>
        <el-form-item label="折扣" prop="ticketDiscount">
          <el-input
            v-model="form.ticketDiscount"
            size="small"
            @keyup.native="limitTicketDiscount(form,'ticketDiscount')"
          ></el-input>折
        </el-form-item>
        <el-form-item label="每人限领" prop="maxTimes">
          <el-select v-model="form.maxTimes" placeholder="请选择">
            <el-option v-for="item in limitNumList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券有效期" prop="validType">
          <el-radio-group v-model="form.validType">
            <el-radio label="2">同活动时间</el-radio>
            <br>
            <el-radio label="0">
              领取后
              <el-input size="small" v-model="form.validDay"></el-input>天有效
            </el-radio>
            <br>
            <el-radio label="1">
              <el-date-picker
                v-model="form.startDate"
                type="datetime"
                placeholder="选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="checkDate"
              ></el-date-picker>
              <el-date-picker
                v-model="form.endDate"
                type="datetime"
                placeholder="选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="checkDate"
              ></el-date-picker>
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="总发放数量" prop="sendCount">
          <el-input v-model="form.sendCount" size="small"></el-input>张 (0张则为不限量)
        </el-form-item>
        <el-form-item label="不可同享优惠" prop="shared">
          <el-radio label="0" v-model="form.shared">无</el-radio>
        </el-form-item>
      </div>
      <div v-if="activeNumber=== 2 && !checkFlag">
        <el-form-item>
          选择商品
          <el-button size="small" type="primary" @click="addGood">添加商品</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="deleteInventoryMany">撤出活动</el-button>
        </el-form-item>
        <el-form-item>
          <el-table
            :data="goodsList.slice((currentPage-1)*pageSize,currentPage*pageSize)"
            style="width: 89%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="barCode" label="商品条码" width="180"></el-table-column>
            <el-table-column prop="primaryClassify" label="商品分类" width="180"></el-table-column>
            <el-table-column prop="brand" label="商品品牌" width="180"></el-table-column>
            <el-table-column prop="name" label="商品名称" width="180"></el-table-column>
            <el-table-column prop="operate" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteInventory(scope.$index,goodsList)">撤出活动</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pageblock">
            <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5,10,15,20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="goodsList.length"
            ></el-pagination>
          </div>
        </el-form-item>
      </div>
      <div v-if="activeNumber=== 3 || checkFlag">
        <el-form-item label="活动名称: ">
          <span>{{form.activityName}}</span>
        </el-form-item>
        <el-form-item label="创建门店: ">
          <span>{{companyName}}</span>
        </el-form-item>
        <el-form-item label="开始时间: ">
          <span>{{form.activityStartTime}}</span>
        </el-form-item>
        <el-form-item label="结束时间: ">
          <span>{{form.activityEndTime}}</span>
        </el-form-item>
        <el-form-item label="定向人群: ">
          <span>全部会员</span>
        </el-form-item>
        <el-form-item label="活动规则: ">
          <div v-html="form.activityDesc"></div>
        </el-form-item>
        <el-form-item label="优惠券类型: ">
          <span>折扣券</span>
        </el-form-item>
        <el-form-item label="折扣: ">
          <span>{{form.ticketDiscount}}折</span>
        </el-form-item>
        <el-form-item label="每人限领: ">
          <span>{{form.maxTimes}}张</span>
        </el-form-item>
        <el-form-item label="优惠券有效期: ">
          <span v-if="form.validType == 2">同活动时间</span>
          <span v-if="form.validType == 0">领取后{{form.validDay}}天有效</span>
          <span v-if="form.validType == 1">{{form.startDate}} 至 {{form.endDate}}</span>
        </el-form-item>
        <el-form-item label="发放数量: ">
          <span v-if="form.sendCount == 0">数量不限</span>
          <span v-else>{{form.sendCount}}张</span>
        </el-form-item>
        <el-form-item label="活动商品: ">
          <el-table
            :data="goodsList.slice((currentPage-1)*pageSize,currentPage*pageSize)"
            style="width: 80%"
          >
            <el-table-column prop="primaryClassify" label="商品分类"></el-table-column>
            <el-table-column prop="name" label="商品名称"></el-table-column>
          </el-table>
          <div class="pageblock">
            <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5,10,15,20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="goodsList.length"
            ></el-pagination>
          </div>
        </el-form-item>
      </div>
    </el-form>
    <div>
      <el-button size="small" @click="routerPush">返回</el-button>
      <el-button
        size="small"
        type="primary"
        @click="backStep"
        v-if="activeNumber != 0 && !checkFlag"
      >上一步</el-button>
      <el-button
        size="small"
        type="primary"
        @click="nextStep"
        v-if="activeNumber != 3 && !checkFlag"
      >下一步</el-button>
      <el-button
        size="small"
        type="primary"
        @click="finishAdd"
        v-if="activeNumber === 3 && !checkFlag"
      >完成</el-button>
      <el-button
        size="small"
        type="primary"
        @click="checkPass(2)"
        v-if="(isStore === '1'|| isPower)&& UnAuditFlag"
      >审核通过</el-button>
      <el-button
        size="small"
        type="primary"
        @click="checkPass(3)"
        v-if="(isStore === '1'|| isPower) && UnAuditFlag"
      >审核驳回</el-button>
    </div>
    <el-dialog
      title="添加商品"
      class="addshopname dialogshopname"
      :visible.sync="dialogTableVisible_good"
      :closeOnClickModal="false"
      :closeOnPressEscape="false"
      width="70%"
    >
      <div class="tableSearch tableSearchS">
        <el-form label-width="80px" class="demo-ruleForm">
          <el-form-item label="商品名称:" label-width>
            <el-input
              class="input"
              v-model="dialogShop.shopName"
              size="small"
              placeholder="输入商品名称或商品条码"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品分类:">
            <el-cascader
              size="small"
              change-on-select
              expand-trigger="hover"
              :options="commodityCategoryfunList"
              v-model="dialogShop.commodityCategory"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="商品品牌:">
            <el-select
              size="small"
              filterable
              v-model="dialogShop.commodityBrand"
              placeholder="请先选择商品品牌"
            >
              <el-option
                v-for="(item,index) in commodityBrandList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label label-width="130px">
            <el-button @click="dialogSearch()" size="small" type="primary">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" size="small" @click="dialogJoinInventoryMany()">参加活动</el-button>
      <el-button
        type="primary"
        size="small"
        @click="dialogDeleteInventoryMany(dialogGoodsList)"
      >撤出活动</el-button>
      <!-- 选择的商品表格 -->
      <div class="tableMain">
        <el-table
          :data="dialogGoodsList"
          tooltip-effect="dark"
          stripe
          style="width: 100%"
          @selection-change="dialogChangeFun_good"
        >
          <!-- ref="dialogChooseData" -->
          <!-- :selectable="puStatus" -->
          <el-table-column type="selection" width="55" align="center" :selectable="puStatus"></el-table-column>
          <el-table-column prop="barCode" label="商品条码" align="center"></el-table-column>
          <el-table-column prop="primaryClassify" label="商品分类" align="center"></el-table-column>
          <el-table-column prop="brand" label="商品品牌" align="center"></el-table-column>
          <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
          <!-- 集团订单新加功能5/31 -->
          <el-table-column prop="name" align="center" label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                :disabled="scope.row.type=='0'"
                v-if="scope.row.isShow==true"
                size="small"
                type="text"
                class="addDetail"
                @click="dialogDeleteInventory(scope.$index,scope.row)"
              >撤出活动</el-button>
              <el-button
                :disabled="scope.row.type=='0'"
                v-if="scope.row.isShow==false"
                size="small"
                type="text"
                class="addDetail"
                @click="dialogJoinInventory(scope.$index,scope.row)"
              >参加活动</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="dialogToPage">确定</el-button>
          <el-pagination
            @size-change="dialogHandleSizeChange"
            @current-change="dialogHandleCurrentChange"
            :current-page="dialogCurrentPage"
            :page-sizes="[5,10,15,20]"
            :page-size="dialogPageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="dialogTotalNum"
          ></el-pagination>
        </div>
      </div>

      <div style="text-align:right;margin-top:20px;">
        <el-button size="small" @click="dialogTableVisible_good = false;changeFlag = true">取 消</el-button>
        <el-button size="small" type="primary" @click="dialogSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import util from "@/util";
import u from "@/util";
import {
  getRepositoryListPage,
  selectByGoodsId,
  getGoodsClassifyTreeList,
  findGoodsRepByGoodsId,
  findGoodsListPage,
  addInventory,
  saveShopActive,
  findShopActivityDetails,
  updateActivityStatus,
  updateShopActivity
} from "@/api";
import { mapState } from "vuex";
export default {
  data() {
    return {
      shopName: "",
      createdTime: "",
      form: {
        activityName: "",
        activityStartTime: "",
        activityEndTime: "",
        activityCrowd: "0",
        activityDesc: "",
        ticketType: "4",
        ticketDiscount: "",
        maxTimes: "",
        validType: "2",
        sendCount: "",
        shared: "0",
        startDate: "",
        endDate: "",
        validDay: ""
      },
      rules: {
        activityName: {
          required: true,
          message: "请输入标题",
          trigger: "blur"
        },
        activityStartTime: {
          type: "string",
          required: true,
          message: "请选择开始日期",
          trigger: ["change", "click"]
        },
        activityEndTime: {
          type: "string",
          required: true,
          message: "请选择结束日期",
          trigger: ["change", "click"]
        },
        activityCrowd: {
          required: true,
          message: "请选择定向人群",
          trigger: "change"
        },
        ticketType: {
          required: true,
          message: "请选择优惠券类型",
          trigger: "change"
        },
        ticketDiscount: {
          required: true,
          message: "请选择折扣",
          trigger: "change"
        },
        maxTimes: {
          required: true,
          message: "请选择每人限领数量",
          trigger: "change"
        },
        validType: {
          required: true,
          message: "请选择优惠券有效期限",
          trigger: "change"
        },
        sendCount: {
          required: true,
          message: "请输入发放总数量",
          trigger: "blur"
        },
        shared: {
          required: true,
          message: "请选择不可同享的优惠",
          trigger: "change"
        }
      },
      activeNumber: 0,
      limitNumList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      goodsList: [],
      selectTableData: [],
      dialogTableVisible_good: false,
      dialogShop: {
        shopName: "", //商品弹窗商品名称
        commodityBrand: "", //品牌
        commodityCategory: [""] //分类
      },
      chooseData: [],
      commodityBrandList: [],
      commodityCategoryfunList: [], //分类数据
      commodityCategoryList: [], //批次数据
      dialogGoodsList: [], //列表数据
      dialogChooseData: [], //弹窗选择的数据
      flag: false,
      startTime: "",
      endTime: "",
      currentPage: 1,
      pageSize: 5,
      dialogCurrentPage: 1,
      dialogPageSize: 5,
      id: "",
      status: "",
      checkFlag: false, // 判断是否是查看, 还是新建
      UnAuditFlag: false, // 判断是否是未审核
      dialogTotalNum: 0,
      goodsId: [],
      isStore: "",
      ticketId: "",
      isPower: 0,
      changeFlag: true
    };
  },
  mounted() {},
  created() {
    this.getNowTime();
    this.id = u.getQuery("id");
    this.status = u.getQuery("status");
    this.isPower = u.getQuery("power");
    if (this.id !== "") {
      this.checkFlag = true;
      findShopActivityDetails({ id: this.id, shopId: this.merchantId }).then(
        res => {
          let data = res.data;
          (this.form.activityName = data.activityName),
            (this.form.activityStartTime = data.activityStartTime),
            (this.form.activityEndTime = data.activityEndTime),
            (this.form.activityCrowd = data.activityCrowd),
            (this.form.activityDesc = data.activityDesc),
            (this.form.ticketType = data.ticketType),
            (this.form.ticketDiscount = data.ticketDiscount),
            (this.form.maxTimes = data.maxTimes),
            (this.form.validType = data.validType),
            (this.form.sendCount = data.sendCount),
            (this.form.startDate = data.startDate),
            (this.form.endDate = data.endDate),
            (this.form.validDay = data.validDay);
          this.isStore = data.isStore;
          this.goodsList = data.goodsList;
          this.ticketId = data.ticketId;
          if (this.form.validType === "2") {
            this.form.startDate = "";
            (this.form.endDate = ""), (this.form.validDay = "");
          }
        }
      );

      if (this.status == "10") {
        this.checkFlag = false;
      }
    }
    if (this.status == 1) {
      this.UnAuditFlag = true;
    }
  },
  computed: mapState(["merchantId", "companyName", "linkName", "power"]),
  methods: {
    getNowTime() {
      let myDate = new Date();
      let nowYear = myDate.getFullYear();
      let nowMonth =
        myDate.getMonth() + 1 < 9
          ? "0" + (myDate.getMonth() + 1)
          : myDate.getMonth() + 1;
      let nowDay =
        myDate.getDate() < 9 ? "0" + myDate.getDate() : myDate.getDate();
      let nowHours =
        myDate.getHours() < 9 ? "0" + myDate.getHours() : myDate.getHours(); //获取当前小时数(0-23)
      let nowMin =
        myDate.getMinutes() < 9
          ? "0" + myDate.getMinutes()
          : myDate.getMinutes(); //获取当前分钟数(0-59)
      let nowSeconds =
        myDate.getSeconds() < 9
          ? "0" + myDate.getSeconds()
          : myDate.getSeconds();
      this.createdTime =
        nowYear +
        "年" +
        nowMonth +
        "月" +
        nowDay +
        "日" +
        nowHours +
        ":" +
        nowMin +
        ":" +
        nowSeconds;
    },
    backStep() {
      this.activeNumber--;
    },
    nextStep() {
      switch (this.activeNumber) {
        case 0:
          this.firstStep();
          break;
        case 1:
          this.secondStep();
          break;
        case 2:
          this.thirdStep();
        default:
          break;
      }
    },
    firstStep() {
      if (!this.form.activityName) {
        this.$message.error("请填写活动标题");
      } else if (!this.form.activityStartTime) {
        this.$message.error("请填写开始时间");
      } else if (!this.form.activityEndTime) {
        this.$message.error("请填写结束时间");
      } else if (!this.form.activityCrowd) {
        this.$message.error("请选择定向人群");
      } else {
        this.activeNumber++;
      }
    },
    secondStep() {
      if (!this.form.ticketType) {
        this.$message.error("请选择优惠券类型");
      } else if (!this.form.ticketDiscount) {
        this.$message.error("请填写折扣");
      } else if (!this.form.maxTimes) {
        this.$message.error("请选择每人限领张数");
      } else if (!this.form.validType) {
        this.$message.error("请选择优惠券有效期");
      } else if (!this.form.sendCount && this.form.sendCount !== 0) {
        this.$message.error("请输入发放优惠券总张数");
      } else if (!this.form.shared) {
        this.$message.error("请选择不可同享优惠");
      } else {
        if (this.form.validType === "0" && !this.form.validDay) {
          this.$message.error("请输入优惠券有效期天数");
        } else if (
          this.form.validType === "1" &&
          (!this.form.startDate || !this.form.endDate)
        ) {
          this.$message.error("请选择优惠券有效时间");
        } else {
          this.activeNumber++;
        }
      }
    },
    thirdStep() {
      if (this.goodsList.length === 0) {
        this.$message.error("请选择参加活动的商品");
      } else {
        this.activeNumber++;
        this.goodsList.forEach(item => {
          this.goodsId.push(item.id);
        });
      }
    },
    addGood() {
      this.dialogTableVisible_good = true;
      this.commodityBrand(); //品牌接口
      this.commodityCategoryfun(); //分类接口
      this.dialogGetGoods(this.dialogPageSize, this.dialogCurrentPage); //弹窗列表数据
    },

    handleSelectionChange(val) {
      if (val) {
        this.chooseData = val;
      } else {
        this.chooseData = [];
      }
    },
    // 单个撤出活动
    deleteInventory(index, rows) {
      this.goodsList.forEach(item => {
        if (item.id == rows[0].id) {
          this.$set(item, "isShow", false);
          rows.splice(index, 1);
        }
      });
    },
    //  多选撤出活动
    deleteInventoryMany() {
      if (this.chooseData.length > 0) {
        let self = this;
        this.goodsList = this.goodsList.filter(function(item) {
          return self.chooseData.indexOf(item) == -1;
        });
      } else {
        this.$message({
          type: "error",
          message: "请先选择撤出活动的商品!"
        });
      }
      this.goodsList.forEach(item => {
        this.chooseData.forEach(ele => {
          if (item.id == ele.id) {
            this.$set(item, "isShow", false);
          }
        });
      });
    },
    // 商品品牌
    commodityBrand() {
      selectByGoodsId({
        repositoryId: ""
      }).then(res => {
        let a = {
          id: "",
          name: "全部"
        };
        res.data.list.unshift(a);
        this.commodityBrandList = res.data.list;
      });
    },
    // 商品品类
    commodityCategoryfun() {
      getGoodsClassifyTreeList().then(res => {
        let a = {
          value: "",
          label: "全部"
        };
        res.data.unshift(a);
        this.commodityCategoryfunList = res.data;
      });
    },

    // 商品列表数据
    dialogGetGoods(pageSize, pageNum) {
      let primaryClassifyId =
        this.dialogShop.commodityCategory[
          this.dialogShop.commodityCategory.length - 1
        ] || "";

      let data = {
        // name: this.dialogShop.shopName,
        // barCode: this.dialogShop.shopName,
        brandId: this.dialogShop.commodityBrand,
        primaryClassifyId: primaryClassifyId,
        shopId: this.merchantId,
        pageSize: pageSize,
        pageNo: pageNum
      };
      if (/^\d+$/.test(this.dialogShop.shopName)) {
        data["barCode"] = this.dialogShop.shopName;
      } else {
        data["name"] = this.dialogShop.shopName;
      }
      findGoodsListPage(data).then(res => {
        if (res.code == 200) {
          this.dialogGoodsList = res.data.list;
          this.dialogGoodsList.forEach(item => {
            this.$set(item, "isShow", false);
          });
        } else {
          this.dialogGoodsList = [];
        }
        // if (this.goodsList.length > 0) {  timeDate
        //   this.goodsList.forEach(item => {
        //     if (item.isShow == true) {
        //       this.dialogGoodsList.unshift(item);
        //     }
        //   });
        //   this.dialogGoodsList = this.uniq(this.dialogGoodsList);
        // }
        this.dialogTotalNum = res.data.total;
      });
    },
    dialogChangeFun_good(val) {
      if (this.changeFlag) {
        if (val) {
          this.dialogChooseData = val;
        } else {
          this.dialogChooseData = [];
        }
      }
    },
    // 搜索
    dialogSearch() {
      this.dialogGetGoods(this.dialogPageSize, this.dialogCurrentPage);
    },
    // 参与活动
    dialogJoinInventoryMany(rows) {
      this.flag = true;
      this.dialogChooseData.forEach(item => {
        this.$set(item, "isShow", true);
      });
    },
    dialogJoinInventory(index, rows) {
      this.flag = true;
      this.$set(rows, "isShow", true);
      this.goodsList.push(rows);
    },
    // 去重
    uniq(arr) {
      let newArr = [arr[0]];
      for (let i = 1; i < arr.length; i++) {
        let repeat = false;
        for (let j = 0; j < newArr.length; j++) {
          if (
            arr[i].id === newArr[j].id &&
            arr[i].goodsBatchNumber === newArr[j].goodsBatchNumber
          ) {
            repeat = true;
            break;
          } else {
          }
        }
        if (!repeat) {
          newArr.push(arr[i]);
        }
      }
      return newArr;
    },
    // 撤出活动
    dialogDeleteInventoryMany(rows) {
      this.flag = true;
      this.dialogChooseData.forEach(item => {
        this.$set(item, "isShow", false);
      });
    },
    dialogDeleteInventory(index, rows) {
      this.flag = true;
      this.$set(rows, "isShow", false);
    },
    // 商品最终选择的值
    dialogSure() {
      if (!this.flag && this.dialogChooseData.length > 0) {
        this.$message.error("请点击参加活动, 再确认");
        return false;
      }
      let goosLists = [];
      this.dialogChooseData.forEach(item => {
        if (item.isShow == true) {
          goosLists.push(item);
        }
      });
      this.goodsList = this.goodsList.concat(goosLists);
      if (goosLists.length == 0) {
        this.$message({
          type: "error",
          message: "请先选择活动的商品!"
        });
      } else {
        this.dialogTableVisible_good = false;
        this.changeFlag = true;
        this.goodsList = this.uniq(this.goodsList);
        this.goodsList.forEach(item => {
          this.$set(item, "isShow", false);
        });
      }
    },
    puStatus(row, index) {
      if (row.type != undefined) {
        return false;
      } else {
        return true;
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    toPage() {},
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    dialogHandleSizeChange(val) {
      this.changeFlag = false;
      this.dialogPageSize = val;
      this.dialogGetGoods(this.dialogPageSize, this.dialogCurrentPage);
    },
    dialogToPage() {
      this.dialogGetGoods(this.dialogPageSize, this.dialogCurrentPage);
    },
    dialogHandleCurrentChange(val) {
      this.changeFlag = false;
      this.dialogCurrentPage = val;
      this.dialogGetGoods(this.dialogPageSize, this.dialogCurrentPage);
    },
    routerPush() {
      util.routerTo("/marketing-center/shop-active/store-active");
    },
    checkTime() {
      if (this.form.activityEndTime === "") {
        return false;
      }
      if (this.form.activityStartTime > this.form.activityEndTime) {
        this.$message.error("开始时间不能大于结束时间");
        this.form.activityEndTime = "";
      }
    },
    checkDate() {
      if (this.form.endDate === "") {
        return false;
      }
      if (this.form.startDate > this.form.endDate) {
        this.$message.error("开始时间不能大于结束时间");
        this.form.endDate = "";
      }
    },

    finishAdd() {
      let data = {
        shopId: this.merchantId,
        activityName: this.form.activityName,
        activityStartTime: this.form.activityStartTime,
        activityEndTime: this.form.activityEndTime,
        activityDesc: this.form.activityDesc,
        activityCreater: this.linkName, // 活动创建人, userName
        activityCrowd: 0, //活动人群, 0全部会员,1注册会员,2会员等级,3自定于会员
        activityCrowdRue: "", //活动人群自定义规则
        activitySource: "", //活动来源,0门店1公司
        goodsId: this.goodsId, //商品id['','']
        ticketType: 4, //优惠券类型
        validType: this.form.validType, // 活动有效期 0 固定天数, 1开始时间结束时间 , 2同活动时间
        validDay: this.form.validDay,
        startDate: this.form.startDate,
        endDate: this.form.endDate,
        sendCount: this.form.sendCount, //总发放数量
        maxTimes: this.form.maxTimes, // 每人限领次数
        ticketDiscount: this.form.ticketDiscount, //折扣
        applyType: 0
      };
      if (this.status == "10") {
        data.id = this.id;
        data.ticketId = this.ticketId;
        updateShopActivity(data).then(res => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.routerPush();
          }
        });
      } else {
        saveShopActive(data).then(res => {
          if (res.code === 200) {
            this.$message.success("新建活动成功");
            this.routerPush();
          }
        });
      }
    },
    // 审核通过
    checkPass(activityAuditState) {
      let tipMessage = "确认通过当前活动吗？";

      if (activityAuditState == 3) {
        tipMessage = "确认驳回当前活动吗？";
      }
      let data = {
        ids: [this.id],
        activityAuditState: activityAuditState
      };
      this.$confirm(tipMessage, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          updateActivityStatus(data).then(res => {
            if (res.code === 200) {
              this.$message.success("操作成功");
              this.routerPush();
            }
          });
        })
        .catch();
    },
    limitName() {
      this.form.activityName = this.form.activityName.replace(/^\s+|\s+$/g, "");
    },
    limitTicketDiscount(obj, key) {
      if (this.form.ticketDiscount > 10) {
        this.form.ticketDiscount = 10;
      } else {
        this.form.ticketDiscount = util.replaceTotal(
          this.form.ticketDiscount,
          2,
          2
        );
      }
    },
    validItem(obj, key) {
      let res = "";
      switch (key) {
        case "priorityRating":
          res = util.replaceNumber(obj[key]);
          break;
        case "price":
          res = util.replaceNum(obj[key], 6);
          break;
      }
      this.$set(obj, key, res);
    }
  }
};
</script>
<style lang="less" scoped>
.pages {
  padding: 15px 20px;
  .el-input {
    width: 200px;
  }
  .el-textarea {
    width: 450px;
  }
  .title {
    height: 40px;
    line-height: 20px;
    font-size: 18px;
  }
  .step-line {
    box-sizing: border-box;
    width: 80%;
    height: 77px;
    padding: 10px 50px;
    // background-color: #fff;
    margin: 0 auto 20px;
  }
  .second-step {
    .el-input {
      margin-right: 5px;
    }
    .el-radio-group {
      .el-radio {
        margin-bottom: 10px;
        .el-input {
          margin-left: 5px;
        }
      }
    }
  }
  .base-info {
    box-sizing: border-box;
    padding: 0 20px;
    width: 80%;
    margin: 0 auto 20px;
    height: 60px;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    background: #fff;
  }
  .pageblock {
    box-sizing: border-box;
    padding-right: 16%;
  }
}
</style>

